﻿using MusicPlayerPlus.Interface.Base;
using MusicPlayerPlus.Interface.Model.Enum;
using MusicPlayerPlus.Interface.Model.Muisc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AMing.Core.Extension;

namespace MusicPlayerPlus.PlugIn.MiniWindow
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, InterfaceBase,
        //Input
        MusicPlayerPlus.Interface.Base.Action.Input.IAutoExecuteAction,
        MusicPlayerPlus.Interface.Base.Action.Input.ITimeLineChangeAction,
        MusicPlayerPlus.Interface.Base.Action.Input.IPlayMusicChange,
        MusicPlayerPlus.Interface.Base.Action.Input.IPlayOrderChangeAction,
        MusicPlayerPlus.Interface.Base.Action.Input.IPlayStateChangeAction,
        MusicPlayerPlus.Interface.Base.Action.Input.IVolumeChangeAction,
        MusicPlayerPlus.Interface.Base.Action.Input.IOrientationActionChange,

        //Output
        MusicPlayerPlus.Interface.Base.Action.Output.IChangeTimeLineAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IPlayAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IPauseAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IStopAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IPreviousTrackAction,
        MusicPlayerPlus.Interface.Base.Action.Output.INextTrackAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IChangeVolumeAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IPlayOrderChangeAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IOrientationAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IAddToMenuAction
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        #region Object

        MediaPlayState mediaPlayState = MediaPlayState.End;

        #endregion

        #region Input

        /// <summary>
        /// 自动执行动作接口
        /// </summary>
        public void AutoExecute()
        {
            //this.Show();
            //this.Hide();
            AddToMenu_Action("迷你窗口", () =>
            {
                this.Show();
                this.WindowState = System.Windows.WindowState.Normal;
            });
        }
        /// <summary>
        /// (改变后事件)进度条改变
        /// </summary>
        /// <param name="max"></param>
        /// <param name="val"></param>
        public void TimeLineChange(double max, double val)
        {
            timeline.Max = max;
            timeline.Value = val;

            TimeSpan ts_max = TimeSpan.FromSeconds(max);
            TimeSpan ts_val = TimeSpan.FromSeconds(val);
            txt_time.Text = string.Format("{0}/{1}", ts_max.ToString_mmss(), ts_val.ToString_mmss());

        }
        /// <summary>
        /// 播放的歌曲改变时
        /// </summary>
        public void PlayMusicChange(MusicItem item)
        {
            if (item != null)
            {
                txt_name.Text = item.Name;
                txt_album.Text = item.AlbumName;
                img_pic.Source = new BitmapImage(new Uri(item.CoveThumbnail));
            }

        }
        /// <summary>
        /// 播放状态改变
        /// </summary>
        public void PlayStateChange(MediaPlayState val)
        {
            mediaPlayState = val;
            if (mediaPlayState == MediaPlayState.Play)
            {
                btn_play.Background = GetImageBrush("img_pause");
            }
            else
            {
                btn_play.Background = GetImageBrush("img_play");
            }
        }
        /// <summary>
        /// (改变后事件)音量改变
        /// </summary>
        public void VolumeChange(double val)
        {
            //vol.Value = val;
        }
        /// <summary>
        /// 播放顺序改变
        /// </summary>
        public void PlayPlayOrderChange(MediaPlayPlayOrder val)
        {

        }
        /// <summary>
        /// 定向通道方法Key
        /// </summary>
        public string OrientationAction_Key { get { return "MiniWindow"; } }
        /// <summary>
        /// 定向通道方法
        /// </summary>
        public void OrientationActionChange(string key, object obj)
        {
            switch (key)
            {
                case "show":
                    this.Show();
                    this.WindowState = System.Windows.WindowState.Normal;
                    break;
                case "hide":
                    this.Hide();
                    this.WindowState = System.Windows.WindowState.Minimized;
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region Output

        /// <summary>
        /// (主动)进度条改变
        /// </summary>
        public Action<double> ChangeTimeLine_Action { private get; set; }
        /// <summary>
        /// 播放
        /// </summary>
        public System.Action Play_Action { private get; set; }
        /// <summary>
        /// 暂停
        /// </summary>
        public System.Action Pause_Action { private get; set; }
        /// <summary>
        /// 停止
        /// </summary>
        public System.Action Stop_Action { private get; set; }
        /// <summary>
        /// 上一首
        /// </summary>
        public System.Action PreviousTrack_Action { private get; set; }
        /// <summary>
        /// 下一首
        /// </summary>
        public System.Action NextTrack_Action { private get; set; }
        /// <summary>
        /// (主动)音量改变
        /// </summary>
        public Action<double> ChangeVolume_Action { private get; set; }
        /// <summary>
        /// 播放顺序改变
        /// </summary>
        public Action<MediaPlayPlayOrder> PlayPlayOrderChange_Action { private get; set; }
        /// <summary>
        /// 触发定向通道
        /// </summary>
        public Action<string, string, object> OrientationAction_Action { private get; set; }
        /// <summary>
        /// 添加菜单动作接口
        /// </summary>
        public Action<string, System.Action> AddToMenu_Action { private get; set; }
        #endregion

        #region UI

        private void btn_pre_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            PreviousTrack_Action();
        }

        private void btn_play_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            if (mediaPlayState == MediaPlayState.Play)
            {
                Pause_Action();
            }
            else
            {
                Play_Action();
            }
        }

        private void btn_stop_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            Stop_Action();
        }

        private void btn_next_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            NextTrack_Action();
        }

        private void img_pic_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        private void btn_goback_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            OrientationAction_Action("MainWindow", "showorhide", this);
            this.WindowState = System.Windows.WindowState.Minimized;
            this.Hide();
        }

        bool isPlay = true;

        private void btn_pre_MouseEnter_1(object sender, MouseEventArgs e)
        {
            btn_pre.Background = GetImageBrush("img_rew_l");
        }

        private void btn_pre_MouseLeave_1(object sender, MouseEventArgs e)
        {
            btn_pre.Background = GetImageBrush("img_rew");
        }

        private void btn_play_MouseEnter_1(object sender, MouseEventArgs e)
        {
            if (mediaPlayState == MediaPlayState.Play)
            {
                btn_play.Background = GetImageBrush("img_pause_l");
            }
            else
            {
                btn_play.Background = GetImageBrush("img_play_l");
            }
        }

        private void btn_play_MouseLeave_1(object sender, MouseEventArgs e)
        {
            if (mediaPlayState == MediaPlayState.Play)
            {
                btn_play.Background = GetImageBrush("img_pause");
            }
            else
            {
                btn_play.Background = GetImageBrush("img_play");
            }
        }

        private void btn_stop_MouseEnter_1(object sender, MouseEventArgs e)
        {
            btn_stop.Background = GetImageBrush("img_stop_l");
        }

        private void btn_stop_MouseLeave_1(object sender, MouseEventArgs e)
        {
            btn_stop.Background = GetImageBrush("img_stop");
        }

        private void btn_next_MouseEnter_1(object sender, MouseEventArgs e)
        {
            btn_next.Background = GetImageBrush("img_next_l");
        }

        private void btn_next_MouseLeave_1(object sender, MouseEventArgs e)
        {
            btn_next.Background = GetImageBrush("img_next");
        }

        private void img_pic_MouseEnter_1(object sender, MouseEventArgs e)
        {
            //btn_pre.Background = GetImageBrush("img_rew_l");
        }

        private void img_pic_MouseLeave_1(object sender, MouseEventArgs e)
        {
            //btn_pre.Background = GetImageBrush("img_rew_l");
        }

        private void btn_goback_MouseEnter_1(object sender, MouseEventArgs e)
        {
            btn_goback.Background = GetImageBrush("img_goback_l");
        }

        private void btn_goback_MouseLeave_1(object sender, MouseEventArgs e)
        {
            btn_goback.Background = GetImageBrush("img_goback");
        }

        private void Window_MouseEnter_1(object sender, MouseEventArgs e)
        {
            ShowButton();
        }

        private void Window_MouseLeave_1(object sender, MouseEventArgs e)
        {
            HideButton();
        }

        /// <summary>
        /// 获取图片资源
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private Brush GetImageBrush(string key)
        {
            Brush brush = Resources[key] as Brush;
            return brush;
        }

        #endregion

        private void ShowButton()
        {
            grid_btn.Visibility = System.Windows.Visibility.Visible;
            txt_name.Visibility = System.Windows.Visibility.Collapsed;
            txt_album.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void HideButton()
        {
            grid_btn.Visibility = System.Windows.Visibility.Collapsed;
            txt_name.Visibility = System.Windows.Visibility.Visible;
            txt_album.Visibility = System.Windows.Visibility.Visible;
        }

    }
}
